<?php
/** @noinspection PhpUnused */
declare(strict_types=1);

namespace app\shop\controller;

use app\shop\exception\FailException;
use app\shop\exception\ParamsErrorException;
use app\shop\logic\RechargeLogic;
use think\db\exception\DataNotFoundException as DataNotFoundExceptionAlias;
use think\db\exception\DbException as DbExceptionAlias;
use think\db\exception\ModelNotFoundException;
use think\helper\Arr;
use think\response\Json;

/**
 * @property RechargeLogic $logic
 */
class RechargeController extends BaseController
{
    public function __construct()
    {
        parent::__construct();

        $this->logic = new RechargeLogic();
    }

    /**
     * 余额充值中心
     * @return Json
     * @throws DataNotFoundExceptionAlias
     * @throws DbExceptionAlias
     * @throws ModelNotFoundException
     */
    public function center(): Json
    {
        $result = $this->logic->rechargeCenter();

        return $this->success($result);
    }

    /**
     * 余额充值操作
     * @return Json
     * @throws ParamsErrorException
     * @throws FailException
     */
    public function recharge(): Json
    {
        $params = $this->postInput(true);

        $this->validateData($params, 'recharge');

        if (!Arr::has($params, 'plan_id') && !Arr::has($params, 'custom_amount')) {
            throw new ParamsErrorException('充值金额有误');
        }

        $result = $this->logic->recharge($params);

        return $this->success($result);
    }

    /**
     * 充值订单列表
     * @return Json
     */
    public function orderList(): Json
    {
        $params = $this->getInput(true);

        $result = $this->logic->getOrderList($params);

        return $this->success($result);
    }
}